package com.apes.fn.scm.retailOrder.repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.query.ApesDao;
import com.apes.framework.util.MapUtil;
import org.springframework.stereotype.Component;

import java.util.Date;

import static com.apes.framework.util.DateUtil.add;
import static com.apes.framework.util.DateUtil.format;

/**
 * @program: apes-server
 * @description 平台引流
 * @author: czs
 * @create: 2021-12-28 16:25
 **/
@Component
public class PlatformFlowDao extends ApesDao {


    /**
     * 未完成/弃用
     */
    public JSONObject getPlatformOrder(JSONObject param){
        StringBuffer stringBuffer = new StringBuffer();
//线上支付引流费用
        stringBuffer.append("SELECT A.ID ID,A.CREATE_DATE ORDER_CREATOR_DATE,B.PLATFORM_ID PLATFORM_ID," +
                "A.DEPT_ID DEPT_ID,(select CAR_LICENSE from FN_CUSTOMER_EXPAND D where A.CUSTOMER_EXPAND_ID = D.ID ) CAR_LICENSE,B.PRODUCT_ID PRODUCT_ID," +
                "B.PRICE_TOTAL SALES_AMOUNT,NVL(C.DISCOUNT_AMOUNT,0) DISCOUNT_AMOUNT,0 PAY_AMOUNT,'' PLATFORM_COUPON_ID," +
                "0 PLATFORM_COUPON_DISCOUNT,0 ONLINE_PAY_FLOW,0 OFFLINE_COMPENSATION_FLOW,0 PLATFORM_COUPON_SUBSIDY," +
                "'online_pay_flow' FLOW_TYPE\n" +
                "FROM RETAIL_ORDER A,\n" +
                "     RETAIL_ORDER_ITEM B\n" +
                "         LEFT JOIN RETAIL_ORDER_DISCOUNT C ON C.RETAIL_ITEM_ID = B.ID\n" +
                "         LEFT JOIN MAS_PRODUCT D ON D.ID = B.PRODUCT_ID\n" +
                "WHERE A.ID = B.SALE_ID\n" +
                "  AND B.MAIN_SALESMAN = '00002256'\n" +
                "  AND C.DISCOUNT_TYPE NOT IN ('001598', '001600')\n" +
                "  AND B.PRICE_TOTAL > 0\n" +
                "  AND A.ORDER_TYPE <> 'ePlus_Return'" +
                "  AND A.DATE_APPROVE > :startDate" +
                "  AND A.DATE_APPROVE <= :endDate") ;

        stringBuffer.append(" UNION ALL ");
        //线下支付引流费
        stringBuffer.append("SELECT A.ID ID,A.CREATE_DATE ORDER_CREATOR_DATE,B.PLATFORM_ID PLATFORM_ID," +
                "A.DEPT_ID DEPT_ID,(select CAR_LICENSE from FN_CUSTOMER_EXPAND D where A.CUSTOMER_EXPAND_ID = D.ID ) CAR_LICENSE,'' PRODUCT_ID," +
                "B.PRICE_TOTAL SALES_AMOUNT,NVL(C.DISCOUNT_AMOUNT,0) DISCOUNT_AMOUNT,(SELECT SUM(B.AMOUNT) FROM RETAIL_ORDER_PAYMENT_METHOD B WHERE B.RETAIL_ORDER_ID = A.ID) PAY_AMOUNT,'' PLATFORM_COUPON_ID," +
                "0 PLATFORM_COUPON_DISCOUNT,0 ONLINE_PAY_FLOW,0 OFFLINE_COMPENSATION_FLOW,0 PLATFORM_COUPON_SUBSIDY," +
                "'offline_compensation_flow' FLOW_TYPE\n" +
                "FROM RETAIL_ORDER A\n" +
                "WHERE A.IS_PLATFORM_FLOW ='1'"+
                "  AND A.DATE_APPROVE > :startDate" +
                "  AND A.DATE_APPROVE <= :endDate") ;
        stringBuffer.append(" UNION ALL ");
        stringBuffer.append("SELECT A.ID ID,A.CREATE_DATE ORDER_CREATOR_DATE,B.PLATFORM_ID PLATFORM_ID," +
                "A.DEPT_ID DEPT_ID,(select CAR_LICENSE from FN_CUSTOMER_EXPAND D where A.CUSTOMER_EXPAND_ID = D.ID ) CAR_LICENSE,B.PRODUCT_ID PRODUCT_ID," +
                "B.PRICE_TOTAL SALES_AMOUNT,NVL(C.DISCOUNT_AMOUNT,0) DISCOUNT_AMOUNT,0 PAY_AMOUNT,'' PLATFORM_COUPON_ID," +
                "0 PLATFORM_COUPON_DISCOUNT,0 ONLINE_PAY_FLOW,0 OFFLINE_COMPENSATION_FLOW,0 PLATFORM_COUPON_SUBSIDY," +
                "'platform_subsidy' FLOW_TYPE\n" +
                "FROM RETAIL_ORDER A\n" +
                "WHERE A.IS_PLATFORM_FLOW ='1'"+
                "  AND A.DATE_APPROVE > :startDate" +
                "  AND A.DATE_APPROVE <= :endDate") ;
        return new JSONObject();
    }


    /**
     * 查询线上支付引流费用()
     * @param param
     * @return
     */
    public JSONArray onlinePayFlow(JSONObject param) {

        StringBuffer stringBuffer = new StringBuffer();

        stringBuffer.append("SELECT A.ID ID,A.CREATE_DATE ORDER_CREATOR_DATE,B.PLATFORM_ID PLATFORM_ID," +
                "A.DEPT_ID DEPT_ID,(select CAR_LICENSE from FN_CUSTOMER_EXPAND D where A.CUSTOMER_EXPAND_ID = D.ID ) CAR_LICENSE,B.PRODUCT_ID PRODUCT_ID," +
                "B.PRICE_TOTAL SALES_AMOUNT,NVL(C.DISCOUNT_AMOUNT,0) DISCOUNT_AMOUNT,0 PAY_AMOUNT,'' PLATFORM_COUPON_ID," +
                "0 PLATFORM_COUPON_DISCOUNT,0 ONLINE_PAY_FLOW,0 OFFLINE_COMPENSATION_FLOW,0 PLATFORM_COUPON_SUBSIDY," +
                "'online_pay_flow' FLOW_TYPE\n" +
                "FROM RETAIL_ORDER A,\n" +
                "     RETAIL_ORDER_ITEM B\n" +
                "         LEFT JOIN RETAIL_ORDER_DISCOUNT C ON C.RETAIL_ITEM_ID = B.ID\n" +
                "         LEFT JOIN MAS_PRODUCT D ON D.ID = B.PRODUCT_ID\n" +
                "WHERE A.ID = B.SALE_ID\n" +
                "  AND B.MAIN_SALESMAN = '00002256'\n" +
                "  AND C.DISCOUNT_TYPE NOT IN ('001598', '001600')\n" +
                "  AND B.PRICE_TOTAL > 0\n" +
                "  AND A.ORDER_TYPE <> 'ePlus_Return'" +
                "  AND A.DATE_APPROVE > :startDate" +
                "  AND A.DATE_APPROVE <= :endDate") ;
        return toJSONArray(find(stringBuffer.toString(), param));

    }

    /**
     * 线下支付引流费
     * @param param
     * @return
     */
    public JSONArray offlineCompensationFlow(JSONObject param) {

        StringBuffer stringBuffer = new StringBuffer();

        stringBuffer.append("SELECT A.ID ID,A.CREATE_DATE ORDER_CREATOR_DATE,B.PLATFORM_ID PLATFORM_ID," +
                "A.DEPT_ID DEPT_ID,(select CAR_LICENSE from FN_CUSTOMER_EXPAND D where A.CUSTOMER_EXPAND_ID = D.ID ) CAR_LICENSE,B.PRODUCT_ID PRODUCT_ID," +
                "B.PRICE_TOTAL SALES_AMOUNT,NVL(C.DISCOUNT_AMOUNT,0) DISCOUNT_AMOUNT,0 PAY_AMOUNT,'' PLATFORM_COUPON_ID," +
                "0 PLATFORM_COUPON_DISCOUNT,0 ONLINE_PAY_FLOW,0 OFFLINE_COMPENSATION_FLOW,0 PLATFORM_COUPON_SUBSIDY," +
                "'offline_compensation_flow' FLOW_TYPE\n" +
                "FROM RETAIL_ORDER A\n" +
                "WHERE A.IS_PLATFORM_FLOW ='1'"+
                "  AND A.DATE_APPROVE > :startDate" +
                "  AND A.DATE_APPROVE <= :endDate") ;
        return toJSONArray(find(stringBuffer.toString(), param));

    }

    /**
     * 平台券补贴
     * @param param
     * @return
     */
    public JSONArray platformCouponSubsidy(JSONObject param) {

        StringBuffer stringBuffer = new StringBuffer();

        stringBuffer.append("SELECT A.ID ID,A.CREATE_DATE ORDER_CREATOR_DATE,B.PLATFORM_ID PLATFORM_ID," +
                "A.DEPT_ID DEPT_ID,(select CAR_LICENSE from FN_CUSTOMER_EXPAND D where A.CUSTOMER_EXPAND_ID = D.ID ) CAR_LICENSE,B.PRODUCT_ID PRODUCT_ID," +
                "B.PRICE_TOTAL SALES_AMOUNT,NVL(C.DISCOUNT_AMOUNT,0) DISCOUNT_AMOUNT,0 PAY_AMOUNT,'' PLATFORM_COUPON_ID," +
                "0 PLATFORM_COUPON_DISCOUNT,0 ONLINE_PAY_FLOW,0 OFFLINE_COMPENSATION_FLOW,0 PLATFORM_COUPON_SUBSIDY," +
                "'platform_subsidy' FLOW_TYPE\n" +
                "FROM RETAIL_ORDER A\n" +
                "WHERE A.IS_PLATFORM_FLOW ='1'"+
                "  AND A.DATE_APPROVE > :startDate" +
                "  AND A.DATE_APPROVE <= :endDate") ;
        return toJSONArray(find(stringBuffer.toString(), param));

    }

}
